<template>
  <div class="login">
    <el-image src="../../../public/static/image/index1.jpg"></el-image>
    <div class="loginPart">
      <h1 class="word">城市绿化管理系统</h1>
          <el-divider />
      <el-form
        action="login"
        method="post"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username" class="inputElement">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入您的用户名"
            prefix-icon="Avatar"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" class="inputElement">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            clearable
            type="password"
            prefix-icon="Lock"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-radio-group color="red" v-model="ruleForm.job">
            <el-radio label="管理员" value="管理员"/>
            <el-radio label="社区管理员"  value="社区管理员"/>
            <el-radio label="维修人员"  value="维修人员"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="captcha" class="inputElement">
          <el-row>
            <el-col :span="16">
              <el-input
                v-model="ruleForm.captcha"
                prefix-icon="Key"
                placeholder="请输入验证码"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <el-image
                style="width: 100px"
                :src="url"
                :fit="fit"
                @click="change()"
                class="pointer"
              ></el-image>
            </el-col>
          </el-row>
        </el-form-item>
        <span style="color: red">{{ msg }}</span>
        <el-form-item>
          <el-button color="#3c9566" plain @click="submit()"
            ><span style="display: inline-block; width: 142px"></span>立即登录<span
              style="display: inline-block; width: 142px"
            ></span
          ></el-button>
          <el-link type="warning" @click="register()">没有账户？去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Register from './register.vue';
export default {
    name: "index",
    data() {
        return {
            ruleForm: {
                username: "aaaa",
                password: "123456",
                job: "社区管理员",
                captcha: "",
            },
            rules: {
                username: [
                    {
                        required: true,
                        message: "用户名不能为空",
                        trigger: "blur",
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "密码不能为空",
                        trigger: "blur",
                    },
                ],
                captcha: [
                    {
                        required: true,
                        message: "验证码不能为空",
                        trigger: "blur",
                    },
                    {
                        min: 4,
                        max: 4,
                        message: "长度为4个字符",
                        trigger: "blur",
                    },
                ],
            },
            url: "http://localhost:8080/login/captcha",
            msg: "",
        };
    },
    methods: {
        submit() {
            this.$axios({
              method:"post",
              url:"http://localhost:8080/login/index",
              data:this.$qs.stringify(this.ruleForm),
            }).then(res => {
              if (res.data.code == 1) {
                this.$store.commit('add',res.data.data);
                this.$router.push("/");
                console.log(this.$store.state.user.id);
              }else{
                this.msg = res.data.msg;
              } 
            });
        },
        change() {
            this.url = "http://localhost:8080/login/captcha?d=" + new Date().getTime();
        },
        register(){
          this.$router.push("/register")
        },
    
    components: { Register },
    change() {
        this.url = "http://localhost:8080/login/captcha?d=" + new Date().getTime()
    },
  }
}
</script>

<style scoped>
.loginPart {
  position: absolute;
  /*定位方式绝对定位absolute*/
  top: 50%;
  left: 50%;
  /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
  transform: translate(-50%, -50%);
  /*实现块元素百分比下居中*/
  width: 450px;
  padding: 40px;
  background: rgba(0, 0, 0, 0.5);
  /*背景颜色为黑色，透明度为0.8*/
  box-sizing: border-box;
  /*box-sizing设置盒子模型的解析模式为怪异盒模型，
    将border和padding划归到width范围内*/
  box-shadow: 0px 15px 25px rgba(1, 0, 0, 0.5);
  /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
  /* border-radius: 15px; */
  /*边框圆角，四个角均为15px*/
}
.loginPart h1 {
  margin: 0 0 30px;
  padding: 0;
  color: #fff;
  text-align: center;
  /*文字居中*/
}
.loginPart .inputbox {
  position: relative;
}
.loginPart .inputElement input {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  letter-spacing: 1px;
  /*字符间的间距1px*/
  margin-bottom: 30px;
  border: none;
  border-bottom: 1px solid #fff;
  outline: none;
  /*outline用于绘制元素周围的线
    outline：none在这里用途是将输入框的边框的线条使其消失*/
  background: transparent;
  /*背景颜色为透明*/
}

button {
  margin-top: 10px;
}

.login {
  width: 100%;
  height: 100%;
  padding: 0px;
}
</style>